package com.yunxin.core.domain.service.contact.impl;

import com.yunxin.core.domain.model.contact.ContactMessageSendSms;
import com.yunxin.core.domain.model.contact.ContactMessageSendSmsRepository;
import com.yunxin.core.domain.model.contact.ContactMessageSendTaskExecuteRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zero°
 * @since 2023-06-15
 */
@Component
@RequiredArgsConstructor
public class SendSmsHandle {

    private final ContactMessageSendSmsRepository contactMessageSendSmsRepository;

    private final ContactMessageSendTaskExecuteRepository messageSendTaskExecuteRepository;

    /**
     * 发送后短信发送记录处理
     * @param contactMessageSendSms contactMessageSendSms
     * @param count count
     */
    @Transactional(rollbackFor = Exception.class)
    public void sendSmsHandle(ContactMessageSendSms contactMessageSendSms, int count) {
        contactMessageSendSmsRepository.updateContactMessageSendSms(contactMessageSendSms);
        messageSendTaskExecuteRepository.updateSuccessCountById(contactMessageSendSms.getTaskExecuteId(), count);
    }

    /**
     * 回调短信发送记录处理
     * @param contactMessageSendSms contactMessageSendSms
     * @param count count
     */
    @Transactional(rollbackFor = Exception.class)
    public void callbackSmsHandle(ContactMessageSendSms contactMessageSendSms, int count) {
        contactMessageSendSmsRepository.updateContactMessageSendSms(contactMessageSendSms);
        messageSendTaskExecuteRepository.updateReachCountById(contactMessageSendSms.getTaskExecuteId(), count);
    }
}
